/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.Permissao;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.TipoUsuario;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "permissao")
public class PermissaoConverter {
    private Permissao entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of PermissaoConverter */
    public PermissaoConverter() {
        entity = new Permissao();
    }

    /**
     * Creates a new instance of PermissaoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public PermissaoConverter(Permissao entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getPermissaoId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getTipoUsuarioCollection();
    }

    /**
     * Creates a new instance of PermissaoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public PermissaoConverter(Permissao entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for permissaoId.
     *
     * @return value for permissaoId
     */
    @XmlElement
    public Integer getPermissaoId() {
        return (expandLevel > 0) ? entity.getPermissaoId() : null;
    }

    /**
     * Setter for permissaoId.
     *
     * @param value the value to set
     */
    public void setPermissaoId(Integer value) {
        entity.setPermissaoId(value);
    }

    /**
     * Getter for pagina.
     *
     * @return value for pagina
     */
    @XmlElement
    public String getPagina() {
        return (expandLevel > 0) ? entity.getPagina() : null;
    }

    /**
     * Setter for pagina.
     *
     * @param value the value to set
     */
    public void setPagina(String value) {
        entity.setPagina(value);
    }

    /**
     * Getter for descricao.
     *
     * @return value for descricao
     */
    @XmlElement
    public String getDescricao() {
        return (expandLevel > 0) ? entity.getDescricao() : null;
    }

    /**
     * Setter for descricao.
     *
     * @param value the value to set
     */
    public void setDescricao(String value) {
        entity.setDescricao(value);
    }

    /**
     * Getter for tipoUsuarioCollection.
     *
     * @return value for tipoUsuarioCollection
     */
    @XmlElement
    public TipoUsuarioesConverter getTipoUsuarioCollection() {
        if (expandLevel > 0) {
            if (entity.getTipoUsuarioCollection() != null) {
                return new TipoUsuarioesConverter(entity.getTipoUsuarioCollection(), uri.resolve("tipoUsuarioCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for tipoUsuarioCollection.
     *
     * @param value the value to set
     */
    public void setTipoUsuarioCollection(TipoUsuarioesConverter value) {
        entity.setTipoUsuarioCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Permissao entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Permissao getEntity() {
        if (entity.getPermissaoId() == null) {
            PermissaoConverter converter = UriResolver.getInstance().resolve(PermissaoConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Permissao entity.
     *
     * @return an resolved entity
     */
    public Permissao resolveEntity(EntityManager em) {
        List<TipoUsuario> tipoUsuarioCollection = entity.getTipoUsuarioCollection();
        List<TipoUsuario> newtipoUsuarioCollection = new java.util.ArrayList<TipoUsuario>();
        for (TipoUsuario item : tipoUsuarioCollection) {
            newtipoUsuarioCollection.add(em.getReference(TipoUsuario.class, item.getTipoUsuarioId()));
        }
        entity.setTipoUsuarioCollection(newtipoUsuarioCollection);
        return entity;
    }
}
